package io.github.harmonly.service.impl;

import io.github.harmonly.entity.activity.Star;
import io.github.harmonly.entity.activity.Activity;
import io.github.harmonly.mapper.ActivityMapper;
import io.github.harmonly.mapper.StarMapper;
import io.github.harmonly.service.StarService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StarServiceImpl implements StarService {

    @Resource
    private StarMapper mapper;
    @Resource
    private ActivityMapper activityMapper;

    @Override
    public Star getStar(int sid) {
        return mapper.getStar(sid);
    }

    @Override
    public List<Star> getStars() {
        return mapper.getStars();
    }

    @Override
    public List<Star> getStarsByUser(int uid) {
        return mapper.getStarsByUser(uid);
    }

    @Override
    public List<Star> getStarsByActivity(int cid) {
        return mapper.getStarsByActivity(cid);
    }

    @Override
    public boolean addStar(Star star) {
        Activity activity = activityMapper.getActivity(star.getCid());
        activity.setStar(activity.getStar() + star.getStar());
        activityMapper.editActivity(activity);
        return mapper.addStar(star) == 1;
    }

    @Override
    public boolean deleteStar(int sid) {
        Star star = getStar(sid);
        Activity activity = activityMapper.getActivity(star.getCid());
        activity.setStar(activity.getStar() - star.getStar());
        activityMapper.editActivity(activity);
        return mapper.deleteStar(sid) == 1;
    }
}
